import json
import os
from tools import *
from pm import manifest_templete


def make_project(path: str):
    os.makedirs(path)
    with open(path+"/"+"manifest.json", "w", encoding="utf-8") as file:
        file.write(json.dumps(manifest_templete.manitemp))
    os.makedirs(path+"/"+"notes")
    os.makedirs(path+"/"+"lines")
    os.makedirs(path+"/"+"config")


def read_project(path: str):
    try:
        with open(path+"/"+"manifest.json", "r", encoding="utf-8") as file:
            manifest = json.loads(file.read())
            info("pm", f"read manifest[{str(manifest)}]")
    except:
        err("pm", "read manifest error")
        return []
    lines_list = []
    notes_list = []
    try:
        notes_list = os.listdir(path+"/"+"notes")
        lines_list = os.listdir(path+"/"+"lines")

    except:
        err("pm", "read notes/lines error")
        return []
    return (manifest, notes_list, lines_list)


def save_manifest(path, manifest):
    with open(path+"/"+"manifest.json", "w", encoding="utf-8") as file:
        file.write(json.dumps(manifest))
        info("pm", f"save manifest[{str(manifest)}]")
